

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>quippy.simplex &mdash; quippy 7ea04c5+ documentation</title>
  

  
  
    <link rel="shortcut icon" href="../../_static/favicon.ico"/>
  
  
  

  
  <script type="text/javascript" src="../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../_static/language_data.js"></script>
        <script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
        <script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"processClass": "math|output_area", "processEscapes": true, "ignoreClass": "document", "inlineMath": [["$", "$"], ["\\(", "\\)"]]}})</script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/theme_overrides.css" type="text/css" />
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../index.html" class="icon icon-home"> quippy
          

          
            
            <img src="../../_static/hybrid.png" class="logo" alt="Logo"/>
          
          </a>

          
            
            
              <div class="version">
                7ea04c5+
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
    
            
            
              
            
            
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../intro.html">Introduction to <code class="docutils literal notranslate"><span class="pre">quippy</span></code></a></li>
<li class="toctree-l1"><a class="reference internal" href="../../install.html">Installation of QUIP and quippy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../Tutorials/index.html">Tutorials</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../quippy.html">Quippy library reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../visualisation.html">Visualisation Tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../fortran_wrapper.html">Appendix: wrapping Fortran 90 Code</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../teach_sparse.html">Command line options of the teach_sparse main program</a></li>
</ul>

            
          
    <p></p>
    <ul>
        <li><a href="../../genindex.html">Index</a></li>
        <li><a href="../../py-modindex.html">Module Index</a></li>
        <li><a href="../../search.html">Search Page</a></li>
        <li><a href="../index.html">Module Source Code</a></li>
    </ul>
  
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">quippy</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../index.html">Module code</a> &raquo;</li>
        
      <li>quippy.simplex</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for quippy.simplex</h1><div class="highlight"><pre>
<span></span><span class="c1"># HQ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   quippy: Python interface to QUIP atomistic simulation library</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   Copyright James Kermode 2010</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   These portions of the source code are released under the GNU General</span>
<span class="c1"># HQ X   Public License, version 2, http://www.gnu.org/copyleft/gpl.html</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   If you would like to license the source code under different terms,</span>
<span class="c1"># HQ X   please contact James Kermode, james.kermode@gmail.com</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   When using this software, please cite the following reference:</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   http://www.jrkermode.co.uk/quippy</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Direct mimimisation and simulated annealing using the downhill simplex</span>
<span class="sd">method of Nelder and Mead.</span>

<span class="sd">Code adapted from chapter 10 of Numerical Recipes (3rd edition).</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;DownhillSimplex&#39;</span><span class="p">]</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>

<span class="k">class</span> <span class="nc">Converged</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Exception raised when convergence achieved.&quot;&quot;&quot;</span>
    <span class="k">pass</span>

<span class="k">class</span> <span class="nc">NotConverged</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Exception raised when convergence fails.&quot;&quot;&quot;</span>
    <span class="k">pass</span>

<div class="viewcode-block" id="DownhillSimplex"><a class="viewcode-back" href="../../simplex.html#quippy.simplex.DownhillSimplex">[docs]</a><span class="k">class</span> <span class="nc">DownhillSimplex</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Multidimensional minimisation using the downhill simplex method of</span>
<span class="sd">    Nelder and Mead.</span>

<span class="sd">    From Numerical Recipes, 3rd edition, section 10.5.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">func</span><span class="p">,</span> <span class="n">x0</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">deltas</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">p</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">args</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">ftol</span><span class="o">=</span><span class="mf">1e-6</span><span class="p">,</span> <span class="n">nmax</span><span class="o">=</span><span class="mi">5000</span><span class="p">,</span> <span class="n">tiny</span><span class="o">=</span><span class="mf">1e-10</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Minimise *func(x)* using a DownhillSimplex.</span>

<span class="sd">        Either the initial simplex *p* or both the starting position</span>
<span class="sd">        *x0* and increments *deltas*. *deltas* can be a single number,</span>
<span class="sd">        in which case it is repeated for all the dimensions in</span>
<span class="sd">        *x0*. *args* can be used to pass extra arguments to *func*.</span>

<span class="sd">        Sets minimisation tolerance to *ftol*, as a fraction of the</span>
<span class="sd">        function value (default 1e-6) and maximum number of function</span>
<span class="sd">        evaluations to *nmax* (default 5000). *tiny* is estimate of</span>
<span class="sd">        machine precision (default 1e-10).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">func</span> <span class="o">=</span> <span class="n">func</span>
        <span class="k">if</span> <span class="n">args</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">args</span> <span class="o">=</span> <span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">args</span> <span class="o">=</span> <span class="n">args</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ftol</span> <span class="o">=</span> <span class="n">ftol</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">NMAX</span> <span class="o">=</span> <span class="n">nmax</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">TINY</span> <span class="o">=</span> <span class="n">tiny</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ybest</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">finfo</span><span class="p">(</span><span class="s1">&#39;d&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">max</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pbest</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">restart</span><span class="p">(</span><span class="n">x0</span><span class="p">,</span> <span class="n">deltas</span><span class="p">,</span> <span class="n">p</span><span class="p">)</span>


    <span class="k">def</span> <span class="nf">restart</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x0</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">deltas</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">p</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">p</span> <span class="o">=</span> <span class="n">p</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">p</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">x0</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">deltas</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;either &quot;p&quot; or both of &quot;x0&quot; and &quot;deltas&quot; must be present&#39;</span><span class="p">)</span>

            <span class="k">try</span><span class="p">:</span>
                <span class="nb">len</span><span class="p">(</span><span class="n">deltas</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">TypeError</span><span class="p">:</span>
                <span class="n">deltas</span> <span class="o">=</span> <span class="p">[</span><span class="n">deltas</span><span class="p">]</span><span class="o">*</span><span class="nb">len</span><span class="p">(</span><span class="n">x0</span><span class="p">)</span>

            <span class="c1"># construct initial simplex</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">p</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">tile</span><span class="p">(</span><span class="n">x0</span><span class="p">,</span> <span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">x0</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">])</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="nb">len</span><span class="p">(</span><span class="n">x0</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">p</span><span class="p">[</span><span class="n">i</span><span class="p">,</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+=</span> <span class="n">deltas</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">mpts</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ndim</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">p</span><span class="o">.</span><span class="n">shape</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">mpts</span><span class="p">)</span>

        <span class="c1"># initial function evaluations</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">mpts</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">func</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">p</span><span class="p">[</span><span class="n">i</span><span class="p">,:],</span> <span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">args</span><span class="p">)</span>


<div class="viewcode-block" id="DownhillSimplex.extrapolate"><a class="viewcode-back" href="../../simplex.html#quippy.simplex.DownhillSimplex.extrapolate">[docs]</a>    <span class="k">def</span> <span class="nf">extrapolate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ihi</span><span class="p">,</span> <span class="n">yhi</span><span class="p">,</span> <span class="n">fac</span><span class="p">,</span> <span class="n">temperature</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Extrapolate by a factor *fac* through the face of the simplex</span>
<span class="sd">        across from the high point *ihi*, try it, and replace the</span>
<span class="sd">        high point if the new point is better. Returns result of</span>
<span class="sd">        evaluating function at new point.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">fac1</span> <span class="o">=</span> <span class="p">(</span><span class="mf">1.0</span> <span class="o">-</span> <span class="n">fac</span><span class="p">)</span><span class="o">/</span><span class="bp">self</span><span class="o">.</span><span class="n">ndim</span>
        <span class="n">fac2</span> <span class="o">=</span> <span class="n">fac1</span> <span class="o">-</span> <span class="n">fac</span>

        <span class="n">ptry</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">p</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span><span class="o">*</span><span class="n">fac1</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">p</span><span class="p">[</span><span class="n">ihi</span><span class="p">,:]</span><span class="o">*</span><span class="n">fac2</span>
        <span class="n">ytry</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">func</span><span class="p">(</span><span class="n">ptry</span><span class="p">,</span> <span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">args</span><span class="p">)</span>

        <span class="c1"># Save best ever value</span>
        <span class="k">if</span> <span class="n">ytry</span> <span class="o">&lt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ybest</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pbest</span> <span class="o">=</span> <span class="n">ptry</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">ybest</span> <span class="o">=</span> <span class="n">ytry</span>

        <span class="k">if</span> <span class="n">temperature</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="c1"># Subtract thermal fluctuation to give the simplex Brownian motion</span>
            <span class="n">ytry</span> <span class="o">=</span> <span class="n">ytry</span> <span class="o">-</span> <span class="p">(</span><span class="o">-</span><span class="n">temperature</span><span class="p">)</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">uniform</span><span class="p">())</span>

        <span class="k">if</span> <span class="n">ytry</span> <span class="o">&lt;</span> <span class="n">yhi</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">[</span><span class="n">ihi</span><span class="p">]</span> <span class="o">=</span> <span class="n">ytry</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">p</span><span class="p">[</span><span class="n">ihi</span><span class="p">,:]</span> <span class="o">=</span> <span class="n">ptry</span>

        <span class="k">return</span> <span class="n">ytry</span></div>

    <span class="k">def</span> <span class="nf">step</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">temperature</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="c1"># Find highest (worst), next-highest and lowest (best) points</span>
        <span class="n">order</span> <span class="o">=</span> <span class="n">y</span><span class="o">.</span><span class="n">argsort</span><span class="p">()</span>
        <span class="n">ilo</span><span class="p">,</span> <span class="n">ihi</span><span class="p">,</span> <span class="n">inhi</span> <span class="o">=</span> <span class="n">order</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">order</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="n">order</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span>

        <span class="c1"># compute fractional range from highest to lowest and</span>
        <span class="c1"># return if done, putting best point and value in slot 0</span>
        <span class="n">rtol</span> <span class="o">=</span> <span class="mi">2</span><span class="o">*</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">y</span><span class="p">[</span><span class="n">ihi</span><span class="p">]</span> <span class="o">-</span> <span class="n">y</span><span class="p">[</span><span class="n">ilo</span><span class="p">])</span><span class="o">/</span>
                  <span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">y</span><span class="p">[</span><span class="n">ihi</span><span class="p">])</span> <span class="o">+</span> <span class="nb">abs</span><span class="p">(</span><span class="n">y</span><span class="p">[</span><span class="n">ilo</span><span class="p">])</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">TINY</span><span class="p">))</span>

        <span class="c1">#print &#39;ylo&#39;, y[ilo], &#39;yhi&#39;, y[ihi], &#39;rtol&#39;, rtol</span>

        <span class="k">if</span> <span class="n">rtol</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">ftol</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">[</span><span class="n">ilo</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">[</span><span class="n">ilo</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">p</span><span class="p">[</span><span class="mi">0</span><span class="p">,:],</span> <span class="bp">self</span><span class="o">.</span><span class="n">p</span><span class="p">[</span><span class="n">ilo</span><span class="p">,:]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">p</span><span class="p">[</span><span class="n">ilo</span><span class="p">,:],</span> <span class="bp">self</span><span class="o">.</span><span class="n">p</span><span class="p">[</span><span class="mi">0</span><span class="p">,:]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">fmin</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="k">raise</span> <span class="n">Converged</span>

        <span class="n">nfunc</span> <span class="o">=</span> <span class="mi">2</span>
        <span class="c1"># new iteration. Start by reflecting simplex from the high point.</span>
        <span class="n">ytry</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">extrapolate</span><span class="p">(</span><span class="n">ihi</span><span class="p">,</span> <span class="n">y</span><span class="p">[</span><span class="n">ihi</span><span class="p">],</span> <span class="o">-</span><span class="mf">1.0</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">ytry</span> <span class="o">&lt;=</span> <span class="n">y</span><span class="p">[</span><span class="n">ilo</span><span class="p">]:</span>
            <span class="c1"># new point is better than best point, so try additional</span>
            <span class="c1"># extrapolation by factor of 2.</span>
            <span class="nb">print</span> <span class="s1">&#39;reflection and expansion&#39;</span><span class="p">,</span> <span class="n">ihi</span>
            <span class="n">ytry</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">extrapolate</span><span class="p">(</span><span class="n">ihi</span><span class="p">,</span> <span class="n">y</span><span class="p">[</span><span class="n">ihi</span><span class="p">],</span> <span class="mf">2.0</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">ytry</span> <span class="o">&gt;=</span> <span class="n">y</span><span class="p">[</span><span class="n">inhi</span><span class="p">]:</span>
            <span class="c1"># Reflected point is worse than second-highest point, so look</span>
            <span class="c1"># for an intermediate lower point, i.e. do a one-dimensional</span>
            <span class="c1"># contraction.</span>
            <span class="nb">print</span> <span class="s1">&#39;contraction&#39;</span><span class="p">,</span> <span class="n">ihi</span>
            <span class="n">ysave</span> <span class="o">=</span> <span class="n">y</span><span class="p">[</span><span class="n">ihi</span><span class="p">]</span>
            <span class="n">ytry</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">extrapolate</span><span class="p">(</span><span class="n">ihi</span><span class="p">,</span> <span class="n">y</span><span class="p">[</span><span class="n">ihi</span><span class="p">],</span> <span class="mf">0.5</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">ytry</span> <span class="o">&gt;=</span> <span class="n">ysave</span><span class="p">:</span>
                <span class="c1"># Can&#39;t get rid of high point. Contract around the lowest point</span>
                <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">mpts</span><span class="p">):</span>
                    <span class="k">if</span> <span class="n">i</span> <span class="o">!=</span> <span class="n">ilo</span><span class="p">:</span>
                        <span class="c1">#print &#39;multiple contraction&#39;, i</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">p</span><span class="p">[</span><span class="n">i</span><span class="p">,:]</span> <span class="o">=</span> <span class="mf">0.5</span><span class="o">*</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">p</span><span class="p">[</span><span class="n">i</span><span class="p">,:]</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">p</span><span class="p">[</span><span class="n">ilo</span><span class="p">,:])</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">func</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">p</span><span class="p">[</span><span class="n">i</span><span class="p">,:],</span> <span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">args</span><span class="p">)</span>
                <span class="n">nfunc</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ndim</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="nb">print</span> <span class="s1">&#39;reflection&#39;</span><span class="p">,</span> <span class="n">ihi</span>
            <span class="n">nfunc</span> <span class="o">-=</span> <span class="mi">1</span> <span class="c1"># correct evaluation count</span>

        <span class="k">return</span> <span class="n">nfunc</span>


<div class="viewcode-block" id="DownhillSimplex.minimise"><a class="viewcode-back" href="../../simplex.html#quippy.simplex.DownhillSimplex.minimise">[docs]</a>    <span class="k">def</span> <span class="nf">minimise</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Run the downhill simplex minimiser.</span>

<span class="sd">        Returns coordinates at minimum as vector *x*. After minimisation,</span>
<span class="sd">        function value at minimum is available as *fmin* attribute,</span>
<span class="sd">        simplex as *p* attribute, and number of function evaluations as</span>
<span class="sd">        *nfunc* attribute.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">nfunc</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">nfunc</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">step</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="o">.</span><span class="n">copy</span><span class="p">())</span>
            <span class="k">except</span> <span class="n">Converged</span><span class="p">:</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">p</span><span class="p">[</span><span class="mi">0</span><span class="p">,:]</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">nfunc</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">NMAX</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">NotConverged</span><span class="p">(</span><span class="s2">&quot;NMAX exceeded&quot;</span><span class="p">)</span></div>


<div class="viewcode-block" id="DownhillSimplex.anneal_step"><a class="viewcode-back" href="../../simplex.html#quippy.simplex.DownhillSimplex.anneal_step">[docs]</a>    <span class="k">def</span> <span class="nf">anneal_step</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">iter</span><span class="p">,</span> <span class="n">temperature</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Anneal for *iter* steps at temperature *temperature*.</span>

<span class="sd">        Returns True if converged to global minimum, False otherwise.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
            <span class="c1"># add positive logarithmically distributed random variable</span>
            <span class="c1"># proportional to temperature to function values at each vertex</span>
            <span class="n">y_fluc</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">+</span> <span class="p">(</span><span class="o">-</span><span class="n">temperature</span><span class="p">)</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="n">size</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">mpts</span><span class="p">))</span>

            <span class="k">try</span><span class="p">:</span>
                <span class="nb">iter</span> <span class="o">-=</span> <span class="bp">self</span><span class="o">.</span><span class="n">step</span><span class="p">(</span><span class="n">y_fluc</span><span class="p">,</span> <span class="n">temperature</span><span class="p">)</span>
            <span class="k">except</span> <span class="n">Converged</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">True</span>

            <span class="k">if</span> <span class="nb">iter</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">False</span></div>


    <span class="k">def</span> <span class="nf">anneal</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">epsilon</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">graphics</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">pause</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="n">step</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">while</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">anneal_step</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">):</span>
            <span class="n">T</span> <span class="o">=</span> <span class="p">(</span><span class="mf">1.0</span> <span class="o">-</span> <span class="n">epsilon</span><span class="p">)</span><span class="o">*</span><span class="n">T</span>
            <span class="n">step</span> <span class="o">+=</span> <span class="n">m</span>
            <span class="k">if</span> <span class="n">graphics</span> <span class="ow">or</span> <span class="n">verbose</span><span class="p">:</span>
                <span class="n">ilo</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="o">.</span><span class="n">argsort</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
                <span class="n">ihi</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="o">.</span><span class="n">argsort</span><span class="p">()[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">graphics</span><span class="p">:</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">gca</span><span class="p">()</span><span class="o">.</span><span class="n">patches</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="mi">1</span><span class="p">:</span>
                    <span class="k">del</span> <span class="n">gca</span><span class="p">()</span><span class="o">.</span><span class="n">patches</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">:]</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">gca</span><span class="p">()</span><span class="o">.</span><span class="n">lines</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="mi">1</span><span class="p">:</span>
                    <span class="k">del</span> <span class="n">gca</span><span class="p">()</span><span class="o">.</span><span class="n">lines</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
                <span class="n">triplot</span><span class="p">(</span><span class="n">ds</span><span class="o">.</span><span class="n">p</span><span class="p">[:,</span><span class="mi">0</span><span class="p">],</span> <span class="n">ds</span><span class="o">.</span><span class="n">p</span><span class="p">[:,</span><span class="mi">1</span><span class="p">],</span> <span class="s1">&#39;r.--&#39;</span><span class="p">)</span>
                <span class="n">draw</span><span class="p">()</span>
                <span class="k">if</span> <span class="n">pause</span><span class="p">:</span>
                    <span class="n">raw_input</span><span class="p">(</span><span class="s1">&#39;continue...&#39;</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">verbose</span><span class="p">:</span>
                <span class="nb">print</span> <span class="s1">&#39;step </span><span class="si">%d</span><span class="s1"> temperature </span><span class="si">%f</span><span class="s1"> x </span><span class="si">%s</span><span class="s1"> y </span><span class="si">%f</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">step</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">p</span><span class="p">[</span><span class="n">ilo</span><span class="p">,:],</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">[</span><span class="n">ilo</span><span class="p">])</span></div>


<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
    
    <span class="n">do_graphics</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="k">if</span> <span class="n">do_graphics</span><span class="p">:</span>
        <span class="kn">from</span> <span class="nn">pylab</span> <span class="k">import</span> <span class="n">plot</span><span class="p">,</span> <span class="n">draw</span><span class="p">,</span> <span class="n">contour</span><span class="p">,</span> <span class="n">clf</span><span class="p">,</span> <span class="n">triplot</span><span class="p">,</span> <span class="n">gca</span>

    <span class="k">def</span> <span class="nf">func</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">cos</span><span class="p">(</span><span class="mf">14.5</span><span class="o">*</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">-</span><span class="mf">0.3</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">+</span><span class="mf">0.2</span><span class="p">)</span><span class="o">*</span><span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">+</span><span class="mf">0.2</span><span class="p">)</span><span class="o">*</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

    <span class="k">if</span> <span class="n">do_graphics</span><span class="p">:</span>
        <span class="n">X</span><span class="p">,</span> <span class="n">Y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mgrid</span><span class="p">[</span><span class="o">-</span><span class="mi">3</span><span class="p">:</span><span class="mi">3</span><span class="p">:</span><span class="mi">100</span><span class="n">j</span><span class="p">,</span><span class="o">-</span><span class="mi">3</span><span class="p">:</span><span class="mi">3</span><span class="p">:</span><span class="mi">100</span><span class="n">j</span><span class="p">]</span>
        <span class="n">clf</span><span class="p">()</span>
        <span class="n">contour</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">Y</span><span class="p">,</span> <span class="n">func</span><span class="p">((</span><span class="n">X</span><span class="p">,</span><span class="n">Y</span><span class="p">)),</span> <span class="mi">10</span><span class="p">)</span>
        <span class="n">draw</span><span class="p">()</span>

    <span class="n">ds</span> <span class="o">=</span> <span class="n">DownhillSimplex</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">x0</span><span class="o">=</span><span class="p">[</span><span class="mf">1.</span><span class="p">,</span><span class="mf">1.</span><span class="p">],</span> <span class="n">deltas</span><span class="o">=.</span><span class="mi">5</span><span class="p">,</span> <span class="n">ftol</span><span class="o">=</span><span class="mf">1e-5</span><span class="p">)</span>
    <span class="n">ds</span><span class="o">.</span><span class="n">anneal</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mf">0.01</span><span class="p">,</span> <span class="n">graphics</span><span class="o">=</span><span class="n">do_graphics</span><span class="p">,</span> <span class="n">pause</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>

    <span class="c1"># restart minimiser from best position found during annealing</span>
    <span class="n">ds</span><span class="o">.</span><span class="n">ftol</span> <span class="o">=</span> <span class="mf">1e-6</span>
    <span class="n">ds</span><span class="o">.</span><span class="n">restart</span><span class="p">(</span><span class="n">x0</span><span class="o">=</span><span class="n">ds</span><span class="o">.</span><span class="n">pbest</span><span class="p">,</span> <span class="n">deltas</span><span class="o">=.</span><span class="mi">1</span><span class="p">)</span>
    <span class="nb">print</span> <span class="n">ds</span><span class="o">.</span><span class="n">minimise</span><span class="p">()</span>

    <span class="c1"># global minimum expected at ~[-0.195, -0.1]</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2008-2016, James Kermode

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>